package cn.tedu.person.controller;

import cn.tedu.person.ex.ServiceException;
import cn.tedu.person.web.JsonResult;
import cn.tedu.person.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import java.util.Set;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleRuntimeException(BindException e) {
        String message=e.getFieldError().getDefaultMessage();
        return JsonResult.fail(ServiceCode.ERR_NOT_FOUND,message);
    }

    //处理权限不足不能访问的错误
    @ExceptionHandler
    public JsonResult handleAccessDeniedException(AccessDeniedException e){
        String message="禁止访问，当前登录的账号无此操作权限!";
        log.debug(message);
        return JsonResult.fail(ServiceCode.ERR_FORBIDDEN,message);
    }

    //处理根据id删除的异常
    @ExceptionHandler
    public JsonResult handleConstraintViolationException(ConstraintViolationException e) {
        String message=null;
        Set<ConstraintViolation<?>> constraintViolations=e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            message = constraintViolation.getMessage();
        }
        return JsonResult.fail(ServiceCode.ERR_CONFLICT,message);
    }

    //处理登录的用户名或者密码错误
    @ExceptionHandler({
            InternalAuthenticationServiceException.class,
            BadCredentialsException.class
    })
    public JsonResult handleAuthenticationException(AuthenticationException e){
        log.warn("程序运行过程中出现了AuthenticationException,将统一处理");
        log.warn("异常类型：{}",e.getClass().getName());
        log.warn("异常信息：{}",e.getMessage());
        String message = "登录失败，用户名或者密码错误";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED,message);

    }

    @ExceptionHandler
    public JsonResult handleDisabledException(DisabledException e){
        log.warn("程序运行过程中出现DisableException,将统一处理");
        log.warn("异常信息：{}",e.getMessage());
        String message="登录失败，账号被禁用！";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED, message);
    }

    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e) {
        String message="服务器忙，请稍后再尝试，或联系系统管理员！（在开发过程中，看到此消息，应该检查出现异常的原因，并补充对此类异常的针对性的处理）";
        log.warn("程序执行过程中出现Throwable！");
        log.warn("异常类型：{}", e.getClass().getName());//可以用这两个
        log.warn("异常类型：{}", e.getMessage());
        e.printStackTrace();
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,message);
    }


}
